/**
 * 
 */
package DB;

import gui.LoaderGif;
import java.util.List;
import datatype.DBtypes.ProgramfromDB;
import main.Session;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.*;
import org.eclipse.wb.swt.SWTResourceManager;
import Utils.Concurrent.ThreadTasks.SearchPersonTask;
import Utils.Concurrent.ThreadTasks.UpdateMediaTask;
import datatype.searchtype.SearchResult.PersonRes;

/**
 * @author Yuval
 * 
 *         -------------------- information ------------------------- - this
 *         class handle creating or editing program - - in the DB, prepare all -
 *         - the GUI all the functions that needed to add actors,- - or creators
 *         the - - class create the "program page", - - that holds the program
 *         info. - ----------------------------------------------------------
 */
public class EditProgram {

	// all the GUI widgets that the Main gui class created for handling the
	// program page, and add events or other stuff
	private Text programName, country, characterName;
	private Spinner start, finish;
	private Spinner numOfSeasons, numOfEpisodes;
	private Table actorTBL, creatorTBL;
	private Button save, cencel, searchActor, searchCreator, setCharacterName;
	private Composite editTV_searchActorComp, editTV_searchCreatorComp,
			searchComp;
	private Canvas loadingCNVS;

	private ProgramfromDB program = null;
	private List<PersonRes> ActorRes = null;
	private List<PersonRes> CreatorRes = null;
	// the search actor and creator widgets

	private Text editTV_searchActorTXT;
	private Table editTV_searchActrorTBL;
	private Text editTV_searchActrorCharaterNameTXT;
	private Button editTV_searchActorGoBTN;
	private Button editTV_searchActorAddBTN;
	private Button editTV_searchActorCencelBTN;
	private Text editTV_searchCreatorTXT;
	private Button editTV_searchCreatorGoBTN;
	private Table editTV_searchCreatorTBL;
	private Button editTV_searchCreatorCencelBTN;
	private Button editTV_searchCreatorAddBTN;

	private SearchPersonTask searchActorTask;
	private SearchPersonTask searchCreatorTask;

	private boolean isActorCompOn = false;
	private boolean isCreatorCompOn = false;
	private Canvas editTV_searchActorLoadingCNVS;
	private Canvas editTV_searchCreatorLoadingCNVS;

	public EditProgram(Text programName, Text country, Text characterName,
			Spinner start, Spinner finish, Spinner numOfSeasons,
			Spinner numOfEpisodes, Table actorTBL, Table creatorTBL,
			Button save, Button cencel, Button setCharacterName,
			Button searchActor, Button searchCreator,
			Composite editTV_searchActorComp,
			Composite editTV_searchCreatorComp, Composite searchComp,
			Canvas loadingCNVS) {
		super();
		this.programName = programName;
		this.country = country;
		this.start = start;
		this.finish = finish;
		this.numOfSeasons = numOfSeasons;
		this.numOfEpisodes = numOfEpisodes;
		this.actorTBL = actorTBL;
		this.creatorTBL = creatorTBL;
		this.save = save;
		this.cencel = cencel;
		this.searchActor = searchActor;
		this.searchCreator = searchCreator;
		this.editTV_searchActorComp = editTV_searchActorComp;
		this.editTV_searchCreatorComp = editTV_searchCreatorComp;
		this.searchComp = searchComp;
		this.loadingCNVS = loadingCNVS;
		this.setCharacterName = setCharacterName;
		this.characterName = characterName;
		prepareSearch();
		prepareWidgets();
	}

	public synchronized ProgramfromDB getProgram() {
		return program;
	}

	public synchronized void setProgram(ProgramfromDB program) {
		this.program = program;
	}

	public synchronized List<PersonRes> getActorRes() {
		return ActorRes;
	}

	public synchronized void setActorRes(List<PersonRes> actorRes) {
		ActorRes = actorRes;
	}

	public synchronized List<PersonRes> getCreatorRes() {
		return CreatorRes;
	}

	public synchronized void setCreatorRes(List<PersonRes> creatorRes) {
		CreatorRes = creatorRes;
	}

	// take all the program info, and put it in the GUI widgets
	public void prepareProgramPage() {
		programName.setText(program.getName());
		program.setOriganCountry(program.getOriganCountry());
		start.setSelection(program.getsYear());
		finish.setSelection(program.getfYear());
		country.setText(program.getOriganCountry());
		numOfEpisodes.setSelection(program.getNumOfEpisodes());
		numOfSeasons.setSelection(program.getNumOfSeasons());
		program.prepareActorTable(actorTBL);
		program.prepareCreatorTable(creatorTBL);

		// finish all ...
	}

	// prepare all the buttons of the editProgram
	private void prepareWidgets() {

		actorTBL.setFocus();
		// only buttons needed
		cencel.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				clearEditTV();
				Session.gui.changeScreenTo(searchComp);
				program = null;
			}
		});

		save.addMouseListener(new MouseAdapter() {
			UpdateMediaTask updateMediaTask = null;
			LoaderGif lg;

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {
				if (lg == null)
					lg = new LoaderGif(loadingCNVS, 1);

				if (updateMediaTask != null)
					Session.pool.killAll(updateMediaTask);

				if (!updateProgram()) {
					Session.gui.showTextBoxWithOk("Missing program name",
							"please enter program name", SWT.ICON_WARNING);
				} else {
					cencel.setEnabled(false);
					searchActor.setEnabled(false);
					searchCreator.setEnabled(false);
					setCharacterName.setEnabled(false);
					clearEditTV();
					updateMediaTask = new UpdateMediaTask(Session.gui
							.getDisplay(), program, lg, true, cencel,
							searchActor, searchCreator, setCharacterName,
							searchComp);
					Session.pool.pushUrgentTask(updateMediaTask,
							lg.getLoaderTask());
				}
			}
		});

		searchActor.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(editTV_searchActorComp, true);
				editTV_searchActorLoadingCNVS.setVisible(false);
				isActorCompOn = true;
				save.setEnabled(false);
				cencel.setEnabled(false);

			}
		});

		searchCreator.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(editTV_searchCreatorComp, true);
				editTV_searchCreatorLoadingCNVS.setVisible(false);
				isCreatorCompOn = true;
				save.setEnabled(false);
				cencel.setEnabled(false);
			}
		});

		editTV_searchCreatorAddBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {

				if (CreatorRes != null) {
					int selection = editTV_searchCreatorTBL.getSelectionIndex();
					if (selection >= 0) {
						PersonRes creator = CreatorRes.get(selection);
						program.addCreator(creator.getId(), creator.getName(),
								false);
						TableItem ti = new TableItem(creatorTBL, SWT.NONE);
						ti.setText(0, creator.getName());
						setVisibilty(editTV_searchCreatorComp, false);
						editTV_searchCreatorTBL.removeAll();
						editTV_searchCreatorTXT.setText("");
						editTV_searchCreatorTXT.setText("");

						isCreatorCompOn = false;

						if (!isActorCompOn && !isCreatorCompOn) {
							save.setEnabled(true);
							cencel.setEnabled(true);
						}
					}
				}
			}

		});

		editTV_searchActorAddBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				if (ActorRes != null) {
					int selection = editTV_searchActrorTBL.getSelectionIndex();
					if (selection >= 0) {
						PersonRes actor = ActorRes.get(selection);
						program.addActor(actor.getId(), actor.getName(),
								editTV_searchActrorCharaterNameTXT.getText(),
								0, 0, false);
						TableItem ti = new TableItem(actorTBL, SWT.NONE);
						ti.setText(0, actor.getName());
						ti.setText(1,
								editTV_searchActrorCharaterNameTXT.getText());
						setVisibilty(editTV_searchActorComp, false);
						editTV_searchActrorTBL.removeAll();
						editTV_searchActrorCharaterNameTXT.setText("");
						editTV_searchActorTXT.setText("");
						isActorCompOn = false;

						if (!isActorCompOn && !isCreatorCompOn) {
							save.setEnabled(true);
							cencel.setEnabled(true);
						}
					}
				}
			}
		});

		editTV_searchActorGoBTN.addMouseListener(new MouseAdapter() {

			LoaderGif lg;

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {

				if (lg == null)
					lg = new LoaderGif(editTV_searchActorLoadingCNVS, 1);

				if (searchActorTask != null)
					Session.pool.killAll(searchActorTask);

				if (!editTV_searchActorTXT.getText().equals("")) {
					searchActorTask = new SearchPersonTask(Session.gui
							.getDisplay(), editTV_searchActorTXT.getText(),
							editTV_searchActrorTBL, "actor", lg,
							editTV_searchActorAddBTN, true);
					editTV_searchActrorTBL.removeAll();
					ActorRes = null;
					lg.showLoader();

					Session.pool.addTaskToPool(true, searchActorTask);
					Session.pool.addTaskToPool(true, lg.getLoaderTask());
				}
			}

		});

		editTV_searchCreatorGoBTN.addMouseListener(new MouseAdapter() {

			LoaderGif lg;

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {
				if (lg == null)
					lg = new LoaderGif(editTV_searchCreatorLoadingCNVS, 1);

				if (searchCreatorTask != null)
					Session.pool.killAll(searchCreatorTask);

				if (!editTV_searchCreatorTXT.getText().equals("")) {
					searchCreatorTask = new SearchPersonTask(Session.gui
							.getDisplay(), editTV_searchCreatorTXT.getText(),
							editTV_searchCreatorTBL, "creator", lg,
							editTV_searchCreatorAddBTN, true);
					editTV_searchCreatorTBL.removeAll();
					CreatorRes = null;
					lg.showLoader();

					Session.pool.addTaskToPool(true, searchCreatorTask);
					Session.pool.addTaskToPool(true, lg.getLoaderTask());
				}
			}

		});

		editTV_searchActorCencelBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(editTV_searchActorComp, false);
				editTV_searchActrorTBL.removeAll();
				editTV_searchActrorCharaterNameTXT.setText("");
				editTV_searchActorTXT.setText("");

				Session.pool.killAll(searchActorTask);
				isActorCompOn = false;

				if (!isActorCompOn && !isCreatorCompOn) {
					save.setEnabled(true);
					cencel.setEnabled(true);
				}
			}

		});

		editTV_searchCreatorCencelBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(editTV_searchCreatorComp, false);
				editTV_searchCreatorTBL.removeAll();
				editTV_searchCreatorTXT.setText("");

				Session.pool.killAll(searchCreatorTask);

				isCreatorCompOn = false;
				if (!isActorCompOn && !isCreatorCompOn) {
					save.setEnabled(true);
					cencel.setEnabled(true);
				}
			}

		});

		setCharacterName.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				int selection = actorTBL.getSelectionIndex();
				if (selection >= 0) {
					program.editActor(program.getActorID(selection),
							program.getActorName(selection),
							characterName.getText());
					actorTBL.getItem(selection).setText(1,
							characterName.getText());
				}
			}

		});

	}

	private void setVisibilty(Composite composite, boolean visi) {

		for (Control c : composite.getChildren()) {
			c.setVisible(visi);
		}
		composite.setVisible(visi);

	}

	// prepare the Program object to send to the DB
	// if the name of the Program is empty, we don't send it
	private boolean updateProgram() {
		if (programName.getText().equals(""))
			return false;
		program.setName(programName.getText());
		program.setNumOfEpisodes(numOfEpisodes.getSelection());
		program.setNumOfSeasons(numOfSeasons.getSelection());
		program.setOriganCountry(country.getText());
		program.setStartDate(start.getSelection(), 0, 0);
		program.setFinishDate(finish.getSelection(), 0, 0);
		return true;

	}

	private void clearEditTV() {

		programName.setText("");
		country.setText("");
		numOfSeasons.setSelection(0);
		numOfEpisodes.setSelection(0);
		start.setSelection(1901);
		finish.setSelection(1901);
		actorTBL.removeAll();
		creatorTBL.removeAll();
		characterName.setText("");
	}

	private void prepareSearch() {

		// prepare the search and add actor window

		editTV_searchActorLoadingCNVS = new Canvas(editTV_searchActorComp,
				SWT.NONE);
		editTV_searchActorLoadingCNVS.setVisible(false);
		editTV_searchActorLoadingCNVS.setBounds(149, 104, 32, 32);

		editTV_searchActorTXT = new Text(editTV_searchActorComp, SWT.BORDER
				| SWT.SEARCH);
		editTV_searchActorTXT.setMessage("search Actor...");
		editTV_searchActorTXT.setFont(SWTResourceManager.getFont("Segoe UI",
				16, SWT.NORMAL));
		editTV_searchActorTXT.setText("");
		editTV_searchActorTXT.setBounds(30, 15, 220, 40);
		editTV_searchActorTXT.setTextLimit(300);

		editTV_searchActrorTBL = new Table(editTV_searchActorComp, SWT.BORDER
				| SWT.FULL_SELECTION);
		editTV_searchActrorTBL.setLinesVisible(true);
		editTV_searchActrorTBL.setHeaderVisible(true);
		editTV_searchActrorTBL.setBounds(30, 60, 270, 120);

		TableColumn tableColumn = new TableColumn(editTV_searchActrorTBL,
				SWT.NONE);
		tableColumn.setWidth(140);
		tableColumn.setText("Actor name");

		TableColumn tableColumn_1 = new TableColumn(editTV_searchActrorTBL,
				SWT.NONE);
		tableColumn_1.setWidth(125);
		tableColumn_1.setText("actin role");

		editTV_searchActorGoBTN = new Button(editTV_searchActorComp, SWT.NONE);
		editTV_searchActorGoBTN.setBounds(260, 15, 40, 40);
		editTV_searchActorGoBTN.setText("GO!");

		editTV_searchActorAddBTN = new Button(editTV_searchActorComp, SWT.NONE);
		editTV_searchActorAddBTN.setBounds(220, 250, 80, 35);
		editTV_searchActorAddBTN.setText("add actor");
		editTV_searchActorAddBTN.setVisible(true);

		editTV_searchActorCencelBTN = new Button(editTV_searchActorComp,
				SWT.NONE);
		editTV_searchActorCencelBTN.setBounds(30, 250, 80, 35);
		editTV_searchActorCencelBTN.setText("cencel");

		editTV_searchActrorCharaterNameTXT = new Text(editTV_searchActorComp,
				SWT.BORDER);
		editTV_searchActrorCharaterNameTXT.setFont(SWTResourceManager.getFont(
				"Segoe UI", 10, SWT.NORMAL));
		editTV_searchActrorCharaterNameTXT.setMessage("character name...");
		editTV_searchActrorCharaterNameTXT.setBounds(30, 215, 200, 25);
		editTV_searchActrorCharaterNameTXT.setTextLimit(256);

		Label editTV_searchActrorCharaterNameLBL = new Label(
				editTV_searchActorComp, SWT.NONE);
		editTV_searchActrorCharaterNameLBL.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_GRAY));
		editTV_searchActrorCharaterNameLBL.setFont(SWTResourceManager.getFont(
				"Segoe UI", 11, SWT.NORMAL));
		editTV_searchActrorCharaterNameLBL.setBounds(30, 185, 135, 25);
		editTV_searchActrorCharaterNameLBL.setText("Character name:");

		// prepare the search and add creator window

		editTV_searchCreatorLoadingCNVS = new Canvas(editTV_searchCreatorComp,
				SWT.NONE);
		editTV_searchCreatorLoadingCNVS.setVisible(false);
		editTV_searchCreatorLoadingCNVS.setBounds(149, 104, 32, 32);

		editTV_searchCreatorTXT = new Text(editTV_searchCreatorComp, SWT.BORDER
				| SWT.SEARCH);
		editTV_searchCreatorTXT.setMessage("search Creator...");
		editTV_searchCreatorTXT.setText("");
		editTV_searchCreatorTXT.setFont(SWTResourceManager.getFont("Segoe UI",
				16, SWT.NORMAL));
		editTV_searchCreatorTXT.setBounds(30, 15, 212, 37);
		editTV_searchCreatorTXT.setTextLimit(200);

		editTV_searchCreatorGoBTN = new Button(editTV_searchCreatorComp,
				SWT.NONE);
		editTV_searchCreatorGoBTN.setText("GO!");
		editTV_searchCreatorGoBTN.setBounds(260, 15, 40, 40);

		editTV_searchCreatorTBL = new Table(editTV_searchCreatorComp,
				SWT.BORDER | SWT.FULL_SELECTION);
		editTV_searchCreatorTBL.setLinesVisible(true);
		editTV_searchCreatorTBL.setHeaderVisible(true);
		editTV_searchCreatorTBL.setBounds(30, 60, 270, 100);

		TableColumn tblclmnCreatorName_1;
		tblclmnCreatorName_1 = new TableColumn(editTV_searchCreatorTBL,
				SWT.NONE);
		tblclmnCreatorName_1.setWidth(148);
		tblclmnCreatorName_1.setText("Creator name");

		TableColumn tableColumn_3 = new TableColumn(editTV_searchCreatorTBL,
				SWT.NONE);
		tableColumn_3.setWidth(115);
		tableColumn_3.setText("known for");

		editTV_searchCreatorCencelBTN = new Button(editTV_searchCreatorComp,
				SWT.NONE);
		editTV_searchCreatorCencelBTN.setText("cencel");
		editTV_searchCreatorCencelBTN.setBounds(30, 170, 80, 35);

		editTV_searchCreatorAddBTN = new Button(editTV_searchCreatorComp,
				SWT.NONE);
		editTV_searchCreatorAddBTN.setVisible(true);
		editTV_searchCreatorAddBTN.setText("add creator");
		editTV_searchCreatorAddBTN.setBounds(210, 170, 90, 35);

		setVisibilty(editTV_searchActorComp, false);
		setVisibilty(editTV_searchCreatorComp, false);
	}

}
